﻿using ET_DbCommon.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET_DbCommon.DAL
{
    public static class Paper_TestQuestionDAL
    {
        /// <summary>
        /// 查找考试试卷
        /// </summary>
        /// <param name="Exam_TestQuestionId"></param>
        /// <returns></returns>
        public static Paper_TestQuestionModel GetMyPaper_TestQuestionModel(int Paper_TestQuestionId)
        {
            Paper_TestQuestionModel paper_TestQuestionModel = new Paper_TestQuestionModel();
            string where = "where Id={0}";
            where = string.Format(where, Paper_TestQuestionId);
            using (var conn = MySqlHelper.GetConnection())
            {
                paper_TestQuestionModel = MySQLExt.GetByWhereFirst<Paper_TestQuestionModel>(conn, where);
                if (paper_TestQuestionModel == null)
                    return null;
                return paper_TestQuestionModel;
            }
        }

        public static List<QuestionIdNameModel> GetQuestionIdNames()
        {
            List<QuestionIdNameModel> lst = new List<QuestionIdNameModel>();
            List<Paper_TestQuestionModel> allModel = GetAll();
            foreach (var questionModel in allModel)
            {
                QuestionIdNameModel model = new QuestionIdNameModel();
                model.Id = questionModel.Id;
                model.TestName = questionModel.TestName;
                lst.Add(model);
            }
            return lst;
        }
        public static List<UMLQuestionModel> GetUMLQuestionList()
        {
            using (var conn = MySqlHelper.GetConnection())
            {
                List<UMLQuestionModel> lst = new List<UMLQuestionModel>();

                List<Paper_TestQuestionModel> allModel = GetAll();
                foreach (var questionModel in allModel)
                {
                    UMLQuestionModel info = new UMLQuestionModel();
                    info.TestQuestionID = questionModel.Id;
                    info.QuestionName = questionModel.TestName;
                    lst.Add(info);
                }
                return lst;
            }

        }

        public static List<Paper_TestQuestionModel> GetAll()
        {
            string sql = " * ";
            var list = AllDefineDAL.GetElementCollection<Paper_TestQuestionModel>(sql);
            return list;
        }

        public static void Delete(int Paper_TestQuestionId)
        {

            using (var conn = MySqlHelper.GetConnection())
            {
                var aaa = MySQLExt.DeleteById<Paper_TestQuestionModel>(conn, Paper_TestQuestionId);

                var tasks = Paper_TestQuestion_TaskDAL.GetMyPaper_TestQuestion_TaskModel(Paper_TestQuestionId);
                foreach (var task in tasks)
                {
                    Paper_TestQuestion_TaskDAL.DeleteByID(task.Id);
                }

            }
        }

        public static void Insert(Paper_TestQuestionModel paper_testQuestionModel, out int newId)
        {
            using (var conn = MySqlHelper.GetConnection())
            {
                newId = MySQLExt.InsertIdentityIndex(conn, paper_testQuestionModel);
            }
        }

        public static int Update(Paper_TestQuestionModel paper_TestQuestionMode)
        {
            using (var conn = MySqlHelper.GetConnection())
            {
                return MySQLExt.UpdateById(conn, paper_TestQuestionMode);
            }
        }

    }
}
